package com.shbt.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.shbt.entity.EntityManagerHelper;
import com.shbt.entity.TReleaseSell;
import com.shbt.idao.ITReleaseSellDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * TReleaseSell entities. Transaction control of the save(), update() and
 * delete() operations must be handled externally by senders of these methods or
 * must be manually added to each of these methods for data to be persisted to
 * the JPA datastore.
 * 
 * @see com.shbt.entity.TReleaseSell
 * @author MyEclipse Persistence Tools
 */
public class TReleaseSellDAO implements ITReleaseSellDAO {
	// property constants
	public static final String RELEASE_SELL_CODE = "releaseSellCode";
	public static final String SENT_ADDRESS = "sentAddress";
	public static final String PAYMENT_MODE = "paymentMode";
	public static final String DELIVER_MODE = "deliverMode";
	public static final String BROWSE_COUNT = "browseCount";
	public static final String COMMENT_COUNT = "commentCount";
	public static final String BOOK_TITLE = "bookTitle";
	public static final String ISBN = "isbn";
	public static final String PRICE = "price";
	public static final String PUBLISH = "publish";
	public static final String AUTHOR = "author";
	public static final String AMOUNT = "amount";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved TReleaseSell entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object)
	 * EntityManager#persist} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * TReleaseSellDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            TReleaseSell entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(TReleaseSell entity) {
		EntityManagerHelper.log("saving TReleaseSell instance", Level.INFO,
				null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent TReleaseSell entity. This operation must be performed
	 * within the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the {@link javax.persistence.EntityManager#remove(Object)
	 * EntityManager#delete} operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * TReleaseSellDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            TReleaseSell entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(TReleaseSell entity) {
		EntityManagerHelper.log("deleting TReleaseSell instance", Level.INFO,
				null);
		try {
			entity = getEntityManager().getReference(TReleaseSell.class,
					entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved TReleaseSell entity and return it or a copy of
	 * it to the sender. A copy of the TReleaseSell entity parameter is returned
	 * when the JPA persistence mechanism has not previously been tracking the
	 * updated entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = TReleaseSellDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            TReleaseSell entity to update
	 * @return TReleaseSell the persisted TReleaseSell entity instance, may not
	 *         be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public TReleaseSell update(TReleaseSell entity) {
		EntityManagerHelper.log("updating TReleaseSell instance", Level.INFO,
				null);
		try {
			TReleaseSell result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public TReleaseSell findById(Integer id) {
		EntityManagerHelper.log("finding TReleaseSell instance with id: " + id,
				Level.INFO, null);
		try {
			TReleaseSell instance = getEntityManager().find(TReleaseSell.class,
					id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all TReleaseSell entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the TReleaseSell property to query
	 * @param value
	 *            the property value to match
	 * @return List<TReleaseSell> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<TReleaseSell> findByProperty(String propertyName,
			final Object value) {
		EntityManagerHelper.log("finding TReleaseSell instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			final String queryString = "select model from TReleaseSell model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<TReleaseSell> findByReleaseSellCode(Object releaseSellCode) {
		return findByProperty(RELEASE_SELL_CODE, releaseSellCode);
	}

	public List<TReleaseSell> findBySentAddress(Object sentAddress) {
		return findByProperty(SENT_ADDRESS, sentAddress);
	}

	public List<TReleaseSell> findByPaymentMode(Object paymentMode) {
		return findByProperty(PAYMENT_MODE, paymentMode);
	}

	public List<TReleaseSell> findByDeliverMode(Object deliverMode) {
		return findByProperty(DELIVER_MODE, deliverMode);
	}

	public List<TReleaseSell> findByBrowseCount(Object browseCount) {
		return findByProperty(BROWSE_COUNT, browseCount);
	}

	public List<TReleaseSell> findByCommentCount(Object commentCount) {
		return findByProperty(COMMENT_COUNT, commentCount);
	}

	public List<TReleaseSell> findByBookTitle(Object bookTitle) {
		return findByProperty(BOOK_TITLE, bookTitle);
	}

	public List<TReleaseSell> findByIsbn(Object isbn) {
		return findByProperty(ISBN, isbn);
	}

	public List<TReleaseSell> findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List<TReleaseSell> findByPublish(Object publish) {
		return findByProperty(PUBLISH, publish);
	}

	public List<TReleaseSell> findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	public List<TReleaseSell> findByAmount(Object amount) {
		return findByProperty(AMOUNT, amount);
	}

	/**
	 * Find all TReleaseSell entities.
	 * 
	 * @return List<TReleaseSell> all TReleaseSell entities
	 */
	@SuppressWarnings("unchecked")
	public List<TReleaseSell> findAll() {
		EntityManagerHelper.log("finding all TReleaseSell instances",
				Level.INFO, null);
		try {
			final String queryString = "select model from TReleaseSell model";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}